<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HashMap 扩容性能优化指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .number-circle {
            width: 48px;
            height: 48px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            font-weight: 700;
            font-size: 1.25rem;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="relative overflow-hidden bg-gradient-to-br from-purple-900 via-purple-800 to-indigo-900 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative container mx-auto px-6 py-24">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                    HashMap 扩容性能优化
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    深入解析大规模数据结构的性能瓶颈与优化策略
                </p>
                <div class="flex justify-center space-x-6 text-lg">
                    <div class="flex items-center">
                        <i class="fas fa-database mr-2"></i>
                        <span>1GB 数据挑战</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-rocket mr-2"></i>
                        <span>7 大优化策略</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-chart-line mr-2"></i>
                        <span>性能提升方案</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0">
            <svg viewBox="0 0 1440 120" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 120L60 105C120 90 240 60 360 45C480 30 600 30 720 37.5C840 45 960 60 1080 67.5C1200 75 1320 75 1380 75L1440 75V120H1380C1320 120 1200 120 1080 120C960 120 840 120 720 120C600 120 480 120 360 120C240 120 120 120 60 120H0Z" fill="#F9FAFB"/>
            </svg>
        </div>
    </section>

    <!-- Problem Overview -->
    <section class="container mx-auto px-6 py-16">
        <div class="max-w-4xl mx-auto">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-6 gradient-text">问题概述</h2>
                <p class="text-lg leading-relaxed text-gray-700 drop-cap">
                    在处理大规模数据结构时，像 HashMap 这样的集合类在遇到扩容操作时可能会对系统性能产生显著影响。特别是当 HashMap 的数据量非常大，比如 1GB 时，扩容操作可能会导致性能瓶颈。HashMap 基于哈希表实现，用于存储键值对，其容量在达到一定阈值时会进行扩容，以保持良好的性能。这一过程涉及到将现有的键值对重新分配到新的、更大的数组中。
                </p>
            </div>
        </div>
    </section>

    <!-- Impact Analysis -->
    <section class="container mx-auto px-6 py-8">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-3xl font-bold text-center mb-12">扩容影响分析</h2>
            <div class="grid md:grid-cols-3 gap-8">
                <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="bg-red-100 p-4 rounded-full mr-4">
                            <i class="fas fa-tachometer-alt text-red-600 text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-bold">性能下降</h3>
                    </div>
                    <p class="text-gray-600">
                        扩容过程中需要将所有现有的键值对重新计算哈希值并移动到新的数组中，这可能会导致严重的性能问题，特别是在高并发的环境中。
                    </p>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="bg-yellow-100 p-4 rounded-full mr-4">
                            <i class="fas fa-memory text-yellow-600 text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-bold">内存消耗</h3>
                    </div>
                    <p class="text-gray-600">
                        扩容过程需要额外的内存来创建新的数组，可能会导致内存占用的短时激增，对系统资源造成压力。
                    </p>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="bg-orange-100 p-4 rounded-full mr-4">
                            <i class="fas fa-ban text-orange-600 text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-bold">请求阻塞</h3>
                    </div>
                    <p class="text-gray-600">
                        在扩容期间，所有对 HashMap 的读写操作可能会被阻塞，导致应用的响应时间变长，影响用户体验。
                    </p>
                </div>
            </div>
        </div>
    </section>

    <!-- Visualization -->
    <section class="container mx-auto px-6 py-16">
        <div class="max-w-6xl mx-auto">
            <div class="bg-gradient-to-r from-purple-50 to-indigo-50 rounded-2xl p-8 md:p-12">
                <h2 class="text-3xl font-bold text-center mb-8">HashMap 扩容机制可视化</h2>
                <div class="mermaid">
                    graph TD
                        A[HashMap 初始状态] --> B{负载因子检查}
                        B -->|超过阈值| C[触发扩容]
                        B -->|未超过| D[正常操作]
                        C --> E[创建新数组]
                        E --> F[重新计算哈希]
                        F --> G[数据迁移]
                        G --> H[更新引用]
                        H --> I[扩容完成]
                        
                        style A fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                        style C fill:#f56565,stroke:#333,stroke-width:2px,color:#fff
                        style I fill:#48bb78,stroke:#333,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>
    </section>

    <!-- Optimization Strategies -->
    <section class="container mx-auto px-6 py-16">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-4xl font-bold text-center mb-4">优化策略</h2>
            <p class="text-xl text-center text-gray-600 mb-12">七大策略助力性能提升</p>
            
            <div class="space-y-8">
                <!-- Strategy 1 -->
                <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                    <div class="flex">
                        <div class="flex-shrink-0 p-8 bg-gradient-to-br from-purple-500 to-indigo-600 flex items-center justify-center">
                            <div class="number-circle">1</div>
                        </div>
                        <div class="p-8 flex-grow">
                            <h3 class="text-2xl font-bold mb-4">预设合适的初始容量</h3>
                            <div class="grid md:grid-cols-2 gap-6">
                                <div>
                                    <h4 class="font-semibold text-lg mb-2 text-purple-700">
                                        <i class="fas fa-chart-pie mr-2"></i>容量规划
                                    </h4>
                                    <p class="text-gray-600">
                                        在创建 HashMap 时，预设一个合适的初始容量，避免频繁扩容。通过估计实际需求的容量来减少扩容次数。
                                    </p>
                                </div>
                                <div>
                                    <h4 class="font-semibold text-lg mb-2 text-purple-700">
                                        <i class="fas fa-sliders-h mr-2"></i>负载因子调整
                                    </h4>
                                    <p class="text-gray-600">
                                        合理设置 HashMap 的负载因子（默认值为 0.75），以平衡内存使用和扩容频率。
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Strategy 2 -->
                <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                    <div class="flex">
                        <div class="flex-shrink-0 p-8 bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center">
                            <div class="number-circle">2</div>
                        </div>
                        <div class="p-8 flex-grow">
                            <h3 class="text-2xl font-bold mb-4">使用 ConcurrentHashMap</h3>
                            <p class="text-gray-600">
                                <i class="fas fa-sync-alt mr-2 text-indigo-600"></i>
                                在高并发环境中，考虑使用 ConcurrentHashMap 替代 HashMap。ConcurrentHashMap 采用了分段锁机制，在扩容时可以减少锁的竞争，优化性能。
                            </p>
                        </div>
                    </div>
                </div>

                <!-- Strategy 3 -->
                <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                    <div class="flex">
                        <div class="flex-shrink-0 p-8 bg-gradient-to-br from-purple-500 to-pink-600 flex items-center justify-center">
                            <div class="number-circle">3</div>
                        </div>
                        <div class="p-8 flex-grow">
                            <h3 class="text-2xl font-bold mb-4">延迟扩容</h3>
                            <p class="text-gray-600">
                                <i class="fas fa-clock mr-2 text-purple-600"></i>
                                在某些情况下，可以使用懒加载的策略来延迟扩容，只有在真正需要时才进行扩容操作。通过合适的策略将扩容操作分摊到多次请求中，减轻单次扩容的压力。
                            </p>
                        </div>
                    </div>
                </div>

                <!-- Strategy 4 -->
                <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                    <div class="flex">
                        <div class="flex-shrink-0 p-8 bg-gradient-to-br from-pink-500 to-red-600 flex items-center justify-center">
                            <div class="number-circle">4</div>
                        </div>
                        <div class="p-8 flex-grow">
                            <h3 class="text-2xl font-bold mb-4">使用其他数据结构</h3>
                            <p class="text-gray-600">
                                <i class="fas fa-database mr-2 text-pink-600"></i>
                                在处理非常大的数据集时，考虑使用其他数据